package com.zhongsou.souyue.log;

import android.content.Context;
import android.provider.ContactsContract;
import android.util.Log;
import com.zhongsou.souyue.filemanager.SaveFile;
import com.zhongsou.souyue.filemanager.SaveType;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/5/29.
 */
public class Logger {
    private static SaveFile mSaveFile;
    private static String mSaveFileName;
    private static boolean mIsShown;
    private static boolean mIsWrite;
    private static Context mContext;
    public static void initLogger(Context context,LoggerConfig loggerConfig){
        if(loggerConfig == null){
            throw new IllegalArgumentException("the LoggerConfig can't be null.  Logger init fail");
        }else{
            mContext = context;
            mSaveFileName = loggerConfig.getmWriteFileName();
            mIsShown = loggerConfig.ismIsShown();
            mIsWrite = loggerConfig.ismIsWrite();
        }

    }

    /**
     * 记录日志
     * @param tag  程序tag
     * @param info  日志info 类名。方法名
     * @param msg  其他消息
     * @param extra  额外信息
     */
    public static void i(String tag,String info,String msg,String... extra){
        i(tag,info,msg,null,extra);
    }
    public static void i(String tag,String info,String msg,Throwable tr,String... extra){
        if(mIsShown){
            Log.i(tag,"info ="+info+"  msg="+msg);
        }
        if(mIsWrite){
            combineJson("info",tag, info, msg,tr, extra);
        }
    }

    public static void e(String tag,String info,String msg,Throwable tr,String... extra){
        if(mIsShown){
            Log.e(tag, "info =" + info + "  msg=" + msg);
        }
        if(mIsWrite) {
            combineJson("error", tag, info, msg,tr, extra);
        }
    }

    public static void combineJson(String level,String tag,String info,String msg,Throwable tr,String extra[]){
        JSONObject obj = new JSONObject();
        try {
            obj.put("t",System.currentTimeMillis());
            obj.put("level",level);
            obj.put("tag",tag);
            obj.put("info",info);
            obj.put("msg",msg);
            if(tr!=null){
                obj.put("error",Log.getStackTraceString(tr).replace("\n\t","|"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        combine(obj,extra);
    }

    public static void combine(JSONObject obj,String extra[]){
        if(extra!=null&&extra.length>0){
            JSONObject object = new JSONObject();
            try {
                for(int i=0;i+1<=extra.length-1;i+=2){
                    object.put(extra[i],extra[i+1]);
                }
                obj.put("extra",object);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        writeToFile(obj);
    }

    public static void writeToFile(JSONObject jsonObj){
        if(mSaveFile!=null){
            mSaveFile.saveDebugLog(jsonObj.toString()+"\r\n");
        }else{
            mSaveFile = new SaveFile(mSaveFileName, SaveType.SAVE_DEBUG_FILE_TYPE);
            mSaveFile.saveDebugLog(jsonObj.toString()+"\r\n");
        }
    }

}
